package com.zhuannimei.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.zhuannimei.GlobalApplication;
import com.zhuannimei.R;
import com.zhuannimei.adapter.OneShopAdapter;
import com.zhuannimei.domain.OneShopProduct;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.parser.OneShopParser;
import com.zhuannimei.service.AsyncDataLoader;
import com.zhuannimei.service.AsyncDataLoader.Callback;
import com.zhuannimei.utility.UrlBank;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.utility.ZnmHttpParams;
import com.zhuannimei.view.ZnmRefreshListView;
import com.zhuannimei.view.ZnmRefreshListView.OnCancelListener;
import com.zhuannimei.view.ZnmRefreshListView.OnRefreshListener;

/**
 * 
 * @author Jack 2014-3-3
 * 
 * 
 */
public class OneYuanShopActivity extends BaseActivity implements
		OnItemClickListener, OnRefreshListener, OnCancelListener {

	private static final String TAG = OneYuanShopActivity.class.getSimpleName();
	private ZnmRefreshListView znmRL;// 赚你妹下拉控件
	private List<OneShopProduct> mList = new ArrayList<OneShopProduct>();
	private int mPage = 0;
	private OneShopAdapter mAdapter;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.oneyuan_shop);
		initView();
		setListener();
		setData();
	}

	private void initView() {
		znmRL = (ZnmRefreshListView) findViewById(R.id.oneyuan_shop_ZR);
	}

	private void setListener() {
		znmRL.setOnItemClickListener(this);
		znmRL.setOnRefreshListener(this);
		znmRL.setOnCancelListener(this);
	}

	private void setData() {
		new AsyncDataLoader(new Callback() {

			@Override
			public void onStart() {
				// 查询
				mList.clear();
				try {
					List<OneShopProduct> list = GlobalApplication.appDB
							.findAll(OneShopProduct.class);
					if (list != null && list.size() != 0) {
						mList.addAll(list);
					}
				} catch (Exception e) {
				}
			}

			@Override
			public void onPrepare() {
				mAdapter = new OneShopAdapter(OneYuanShopActivity.this,
						mList);
				znmRL.setAdapter(mAdapter);
				znmRL.onManualRefresh();
			}

			@Override
			public void onFinish() {
				WuLog.i(TAG, "查询结束");
					WuLog.i(TAG, "数据库初始化列表");
				getDataFormNet();
			}
		}).execute();
	}

	@Override
	protected void onStart() {
		super.onStart();
		mTitlePane.setTitle("一元购");
	}

	@Override
	public void onCancel() {
		WuLog.i(TAG, "取消刷新");
		
		znmRL.onRefreshComplete();
	}

	@Override
	public void onRefresh() {
		WuLog.i(TAG, "刷新");
		getDataFormNet();
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		WuLog.i(TAG, "当前点击的位置："+arg2);
		OneShopProduct osp = mList.get(arg2-1);
		Intent intent = new Intent(OneYuanShopActivity.this,LotteryDetailActivity.class);
		intent.putExtra(LotteryDetailActivity.SHOP_PHASE, osp.getPhase());
		intent.putExtra(LotteryDetailActivity.SHOP_LUCKID, osp.getLuckId());
		intent.putExtra(LotteryDetailActivity.SHOP_CYID, osp.getCgyId());
		startActivity(intent);
	}

	public void getDataFormNet() {
		RequestParams mParams = ZnmHttpParams.getDefaultRequestParams();
		mParams.addBodyParameter("pageIndex", "" + mPage);
		RequestServerData.getLuckList(mParams, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				WuLog.i(TAG, "" + responseInfo.result);
				String results = responseInfo.result;
				try {
					JSONObject jo = new JSONObject(results);
					if ("0".equals(jo.optString("code"))) {
						// 成功
						List<OneShopProduct> list = new OneShopParser()
								.parseJSON(results);
						WuLog.i(TAG, "解析后集合的大小==" + list.size());
						if (list != null && list.size() != 0) {
							mList.clear();
							mList.addAll(list);
							WuLog.i(TAG, "list==");
						}
						mAdapter.notifyDataSetChanged();
						//先删后存
						GlobalApplication.appDB.deleteByWhere(OneShopProduct.class, null);
						for (OneShopProduct osp : mList) {
							GlobalApplication.appDB.save(osp);
						}
					} else {
						showCustomToast("网络不给力");
					}
				} catch (Exception e) {
					e.printStackTrace();
					showCustomToast("网络不给力");
				}
				znmRL.onRefreshComplete();
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				WuLog.i(TAG, "getExceptionCode==" + error.getExceptionCode()
						+ ";" + msg);
				showCustomToast("网络不给力");
			}
		});
	}
}